
********************************************************************************************
*Appendix Table 8: Individual fixed effects regression model on labor supply, work time, and 
*                  earned income considering information from primary and secondary jobs 
********************************************************************************************

version 15.1
cd "${mypath}\CHNS_project\01_data\02_posted\"

log using "${mypath}\CHNS_project\03_log_files\13_appendix_table_8.log", replace

use CHNS_1993_2015_20_imputed.dta,clear
mi import ice, automatic

*generate exclusion condition, that is, people who work in agriculture in urban area
gen be_farmer = (migrant_group != 2 & occupation == 3 & work_unit == 4) if be_employed == 1

*generate annual salary last year(log) for primary and secondary jobs
gen wage_i_1 = exp(log_wage_1)*workmonths1+exp(log_bonuse_1) if employed_last1==1
gen wage_i_2 = exp(log_wage_2)*workmonths2+exp(log_bonuse_2) if employed_last2==1
gen wage_i = wage_i_1 + wage_i_2
replace wage_i = wage_i_1 if !mi(wage_i_1) & mi(wage_i_2)
replace wage_i = wage_i_2 if mi(wage_i_1) & !mi(wage_i_2)
gen log_wage_i = log(wage_i)

*generate hourly wage last year(log) for primary and secondary jobs
gen log_hour_wage_1 = log_wage_1-log((365/12/7)*workdays1*workhours1) if employed_last1==1
gen log_hour_wage_2 = log_wage_2-log((365/12/7)*workdays2*workhours2) if employed_last2==1
gen hour_wage_1 = exp(log_hour_wage_1)
gen hour_wage_2 = exp(log_hour_wage_2)
gen hour_wage = hour_wage_1 + hour_wage_2
replace hour_wage = hour_wage_1 if !mi(hour_wage_1) & mi(hour_wage_2)
replace hour_wage = hour_wage_2 if mi(hour_wage_1) & !mi(hour_wage_2)
gen log_hour_wage = log(hour_wage)

*generate working hours for primary and secondary jobs
gen hours_lastw = hours_lastw1 + hours_lastw2
replace hours_lastw = hours_lastw1 if !mi(hours_lastw1) & mi(hours_lastw2)
replace hours_lastw = hours_lastw2 if mi(hours_lastw1) & !mi(hours_lastw2)

*generate "over the statutory hours last week"
gen over_lastw = hours_lastw > 44

*generate occupations
tab occupation if be_employed == 1 ,gen(occ)
la var occ2 "officer"
la val occ2 occ2
la var occ3 "farmer fisherman and hunter"
la val occ3 occ3
la var occ4 "skilled worker"
la val occ4 occ4
la var occ5 "non-skilled worker"
la val occ5 occ5
la var occ6 "service worker"
la val occ6 occ6
la var occ7 "other(including homemaker)"
la val occ7 occ7

local outcome_1 "hours_lastw"
local outcome_2 "over_lastw"
local outcome_3 "log_wage_i"
local outcome_4 "log_hour_wage"

local inde "year1997 year2000 year2004 year2006 year2009 year2011 year2015 be_migrant be_migrant##year1997 be_migrant##year2000 be_migrant##year2004 be_migrant##year2006 be_migrant##year2009 be_migrant##year2011 be_migrant##year2015"
local control "age26_30 age31_35 age36_40 age41_45 age46_50 age51_55 year_education marital_status h_group1 h_group2 h_group4 h_group5 num_child num_elderly population pcgdp child_elder percent_farmer service_sector"
            
*generate  employment for primary and secondary jobs
gen employed_last = 1 if employed_last1==1 | employed_last2==1
replace employed_last = 0 if mi(employed_last)

*1.predict inverse mills ratio
prob employed_last year1997 year2000 year2004 year2006 year2009 year2011 year2015 be_migrant be_migrant##year1997 be_migrant##year2000 be_migrant##year2004 be_migrant##year2006 be_migrant##year2009 be_migrant##year2011 be_migrant##year2015 age26_30 age31_35 age36_40 age41_45 age46_50 age51_55 gender ethnicity year_education marital_status h_group1 h_group2 h_group4 h_group5 num_child num_elderly population pcgdp child_elder percent_farmer service_sector Liaoning Heilongjiang Shandong Henan Hubei Hunan Guangxi Guizhou if be_rural_resident == 0 & time != 1 & be_farmer !=  1
predict gw_urban, xb
gen lambda_urban = normalden(gw_urban)/normal(gw_urban) if employed_last == 1 & be_rural_resident == 0 & time != 1 & be_farmer !=  1

local path "${mypath}\CHNS_project\04_tables\appendix_table_8\"

*declare imputated data as panel data
mi xtset IDind wave

*fixed-effect model considering information from primary and secondary jobs 
forv i = 1/4 {
	quiet eststo: mi estimate,post esampvaryok: xtreg `outcome_`i'' `inde' `control' lambda_urban if employed_last == 1 & be_rural_resident == 0 & time != 1 & be_farmer !=  1 & degree_education <= 3,fe r
	est store outcome`i',title(outcome`i')
}
esttab outcome1 outcome2 outcome3 outcome4 using "`path'appendix_table_8.csv", constant b(3) star(+ 0.10 * 0.05 ** 0.01) se(%9.3f) r2 replace nogap nonumbers noomitted stats(N_mi)

log close
